<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class User extends CI_Controller {

    /**
     * Action login by user name and password
     * @author tien.nguyen
     */
    public function login(){
        $this->load->model('Users', '', true);
        if($this->input->post()){
            $data = $this->input->post();
            //var_dump($this->Users->login($data['username'], $data['password'])); die;
            if($this->Users->login($data['username'], $data['password'])){
                redirect('/home');   
            }else{
                $this->session->set_flashdata('message', 'Thông tin đăng nhập không đúng.');
                redirect('/user/login');
            }
        }    
        $this->load->view('user/login');
    }
    
    /**
     * Logout
     * @author tien.nguyen
     */
    public function logout(){
        $removeSession = array('UserId', 'UserName', 'Password', 'Enable', 
            'CreatedDate', 'Email', 'Phone', 'Address', 'Role');
         
        //loop value session user and unset it (set value is null)   
        foreach($removeSession as $item){
            $this->session->set_userdata($item, null);
        } 
        redirect('/user/login');
    }
    
    /**
     * User registry the system
     * @author tien.nguyen
     */
    public function registry(){
        $this->load->model('Users', '', true);
        $data = array();
        
        //insert data
        if($this->input->post()){
            $data = $this->input->post();
            $data['CreatedDate'] = date('Y-m-d H:i:s');
            $data['UserName'] = $data['Email'];
            if($this->_validRegistry($data)){
                
                if($this->Users->registry($data)){
                    redirect('/user/login');
                }
            }
        }
        
        $this->load->view('user/registry', $data);
    }
    
    /**
     * validate registry for data
     * @author tien.nguyen
     */
    private function _validRegistry($data){
        $error = 0;
        $this->load->model('Users', '', true);
        if($this->Users->getUserByUsername($data['UserName'])){
            $this->session->set_flashdata('message_username', 'Tài khoản đã tồn tại.');
            $error++;
        }

        if($this->Users->getUserByEmail($data['Email'])){
            $this->session->set_flashdata('message_email', 'Email đã tồn tại.');
            $error++;
        }
        return $error == 0;
    }
    
    /**
     * show information of user
     * @author tien.nguyen
     */
    public function detail(){
        $this->load->model('Users', '', true);
        $user = $this->Users->getUserByUserId($this->session->userdata['UserId']);
        $data['User'] = $user;
        $this->load->view('user/detail', $data);
    }
    
    /**
     * Action edit user
     * @author tien.nguyen
     */
    public function edit(){
        $this->load->model('Users', '', true);
        
        //update information user
        if($this->input->post()){
            $data = $this->input->post();
            if($this->Users->edit($data, $this->session->userdata['UserId'])){
                redirect('user/detail');
            }
        }
        
        $user = $this->Users->getUserByUserId($this->session->userdata['UserId']);
        $data['User'] = $user;
        $this->load->view('user/edit', $data);
    }
    
    /**
     * change password
     * @author tien.nguyen
     */  
    public function change_password(){
        $this->load->model('Users', null, true);
        
        //set post when post
        if($this->input->post()){
            $data = $this->input->post();
            $res = $this->Users->changePassword($data['oldPass'], $data['newPass']);
            
            $this->session->set_flashdata('message', $res['message']);
            $this->session->set_flashdata('class_message', ($res['save'] ? 'warning' : 'error'));
            redirect('user/change_password');
        }
        
        $this->load->view('user/change-password');
    }
}